package com.bow.syntax

/**
  * @author vv
  * @since 2017/7/24.
  */
object ClassDemo {

  def main(args: Array[String]) {
    val p1 = new Person
    println(p1.age) //18
    p1.increment()
    println(p1.getAge) // 19

    val s1 = new Student
    println(s1.getName) // clowmale

    val t1 = new Teacher(27, "vv", "NN")
    val t2 = new Teacher("NN") //通过主构造器构造
    println(t1.name) // vv
    println(t2.school) //NN

  }
}

//类默认是public级别的
class Person {
  var age = 18 //字段必须得初始化()
  def getAge = age //这个是方法，没有参数可以省略()
  def increment() {
    this.age += 1
  }
}


class Student {
  private[this] var gender = "male" //private[this] 只有该类的this可以使用
  private var name = "clow"

  //自定义getter/setter
  def getName = this.name + this.gender

  def setName(value: String) {
    this.name = value
  }
}

//主构造器跟在类名后面
class Teacher(var school: String) {
  var age: Int = _
  var name: String = _ //可以预留

  //重载的构造器
  def this(age: Int, name: String, school: String) {
    this(school) //必须先调主构造器
    this.age = age
    this.name = name
  }
}
